package cn.mineserv.video.videoordersystem.utils;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.DigestUtils;

import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.UUID;

public class Util {

    /**
     * 随机字符串
     * @return 随机字符串
     */
    public static String generateUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * MD5加密
     * @param string 字符串
     * @return 加密后的字符串
     */
    public static String md5(String string) {
        if (StringUtils.isBlank(string)) {
            return null;
        }
        return DigestUtils.md5DigestAsHex(string.getBytes());
    }

    /**
     * 根据参数生成签名
     * @param appId 应用ID
     * @param appSecretKey 应用密钥
     * @param timestamp 时间戳
     * @return 签名
     */
    public static String generateSign(String appId, String appSecretKey,long timestamp) {
        return md5(appId + appSecretKey + timestamp);
    }
    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return sb.toString();
    }

    public static Map<String,Object> readJsonFromUrl(String url) throws Exception {
        Gson gson = new Gson();
        return gson.fromJson(readTextFromUrl(url), new TypeToken<Map<String,Object>>(){}.getType());
    }
    public static String readTextFromUrl(String url) throws Exception {
        try (InputStream is = new URL(url).openStream()) {
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
            return readAll(rd);
        }
        // System.out.println("同时 从这里也能看出 即便return了，仍然会执行finally的！");
    }
    public static String getIp() {
        String ip = null;
        try {
            ip = readTextFromUrl("http://ip.3322.net");
        } catch (Exception e) {
            e.printStackTrace();
        }
/*        if (request == null) {
            return "";
        }
        ip = request.getHeader("X-Requested-For");
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if("0:0:0:0:0:0:0:1".equals(ip)){
            return "127.0.0.1";
        }*/

        return ip;
    }


    public static boolean isLocalHost(String ip){
        return "127.0.0.1".equals(ip) || "localhost".equals(ip);
    }
}
